
<?php

	require_once("userclass.php");
	$objUser = new userClass();
	
	require_once("wardclass.php");
	$objWard = new wardClass();
		
	
	$type = $_REQUEST['type'];
	if($type == 'customer')
		$page_head = 'User Info';
	else
		$page_head = 'Photographer/Designer';
	$addme=$_POST["addme"];
	global $wpdb;
     	 
	if(isset($_REQUEST['create_new_user'])){
			 $error  = '';
			 $user_name  = $_REQUEST['user_name']  ? $_REQUEST['user_name']  : '';
			 $user_email = $_REQUEST['user_email'] ? $_REQUEST['user_email'] : '';
			 $password   = $_REQUEST['password'];
			 $first_name = $_REQUEST['first_name'] ? $_REQUEST['first_name'] : '';
			
			 $last_name  = $_REQUEST['last_name']  ? $_REQUEST['last_name']  : '';
			 $address    = $_REQUEST['address']    ? $_REQUEST['address']    : '';
			 $city   	 = $_REQUEST['city']       ? $_REQUEST['city']       : '';
			 $state   	 = $_REQUEST['state']      ? $_REQUEST['state']      : '';
			 $zip   	 = $_REQUEST['zip']		   ? $_REQUEST['zip']        : '';
			 $phone   	 = $_REQUEST['phone']	   ? $_REQUEST['phone']		 : '';
			 $confirm_password = $_REQUEST['confirm_password'];
			 
			 if($user_name==''){
				 $error.= ' User filed is blank';
				 }
			 if($user_email==''){
				 $error.= ' <br> Email filed is blank';
				 }	 
			 if($password==''){
				 $error.= '<br> Password filed is blank';
				 }	 
			 if($first_name==''){
				 $error.= '<br> Name filed is blank';
				 }	
			 if($password!=	 $confirm_password){
				  $error.= '<br> Passwords do not match';
				 }
			 
			 
			 $userdata = array(
				'user_login'   => $user_name,
				'user_pass'    => $password,
				'first_name'   => $first_name,
				'last_name'	   => $last_name,
				'user_email'   => $user_email
				
			);
				
			 if( $error ==''){
				 # CREATE A NEW USER
				 $user_id = username_exists( $user_name );
					if ( !$user_id and email_exists($user_email) == false ) {
						//$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
						// $user_id = wp_create_user( $user_name, $password, $user_email );
							$user_id = wp_insert_user( $userdata );
							$objUser->updateUserInfo($user_id,'address',$address);
							$objUser->updateUserInfo($user_id,'city',$city);
							$objUser->updateUserInfo($user_id,'state',$state);
							$objUser->updateUserInfo($user_id,'zip',$zip);
							$objUser->updateUserInfo($user_id,'phone',$phone);
		
						  # SET USER ROLE 
							 $user = new WP_User( $user_id );
							if(isset($_POST['user_type'])){
							
								if($_POST['user_type']=='photographer')
									 $user->set_role( 'photographer' );
								else
									 $user->set_role( 'designer' );
								
							}
							else
							 $user->set_role( 'customer' );
									
							$message  = ' User name: '.$user_name;
							$message .= ' <br>Password: '.$password;
							
							
							$objTrack->send_mail_track($to=$user_id,$subject,$message,$headers,$attachments,$from,$order_id,$mailtype='login_informations');
							echo '<script>
									window.location.href = "'.get_site_url().'/wp-admin/admin.php?page=memberlist&info=saved&type='.$type.'"; 
							</script>';
							exit;
						
						
							//if(isset($user->caps["photographer"])){
								
							//else if(isset($user->caps["designer"])){
								
					} 
					else {
						 $error = ('User already exists. Choose another Username.');
						 $user_name = ''; 
						
					}
					
			 }
			 
			
			 
	
	}		 
	if(isset($_POST['save_customer_info'])){
		
			$user_id		 		= $_REQUEST['user_id'];	
			$name 			 		= $_REQUEST['name'];
			$baptismal_name	 		= $_POST['baptismal_name'];
			$family_position 		= $_POST['family_position'];
			$date_of_birth	 		= $_POST['date_of_birth'];
			$baptism_date	 		= $_POST['baptism_date'];
			$confirmation_date		 = $_POST['confirmation_date'];
			$name_father 		 = $_POST['name_father'];
			$name_mother		 = $_POST['name_mother'];
			$phone_no 		 = $_POST['phone_no'];
			
			$mob_no 		 = $_POST['mob_no'];
			$e_mail 		 = $_POST['e_mail'];
			$blood_group 	 = $_POST['blood_group'];
			
			
			
			$present_address 	 = $_POST['present_address'];
			
			
			
			/*$objUser->updateUserInfo($user_id,'address',$address);
			$objUser->updateUserInfo($user_id,'first_name',$first_name);
			$objUser->updateUserInfo($user_id,'last_name',$last_name);
			$objUser->updateUserInfo($user_id,'city',$city);
			$objUser->updateUserInfo($user_id,'state',$state);
			$objUser->updateUserInfo($user_id,'zip',$zip);
			$objUser->updateUserInfo($user_id,'phone',$phone);*/
		
			mysql_query("UPDATE  `parishioner` SET  
							`name` =  '".$name."',
							`baptismal_name` =  '".$baptismal_name."',
							`family_position` =  '".$family_position."',
							`date_of_birth` =  '".$date_of_birth."',
							`baptism_date` =  '".$baptism_date."',
							`confirmation_date` =  '".$confirmation_date."',
							`name_father` =  '".$name_father."',
							`name_mother` =  '".$name_mother."',
							`phone_no` =  '".$phone_no."',
							`mob_no` =  '".$mob_no."',
							`e_mail` =  '".$e_mail."',
							`present_address` =  '".$present_address."',
							
							`blood_group` =  '".$blood_group."'
							
							
						 WHERE  `parishioner`.`id` =".$user_id.";");
			echo '<script>
						 window.location.href = "'.get_site_url().'/wp-admin/admin.php?page=memberlist&info=upd"; 
				</script>';
			exit;
		}
	/*if($addme==1)
	{
		$objMem->addNewMember($table_name = $wpdb->prefix . "member",$_POST);
		//header("Location:admin.php?page=myplug/muyplg.php&info=saved");
		//exit;
	}
	else if($addme==2)
	{
		$objMem->updMember($table_name = $wpdb->prefix . "member",$_POST);
		//header("Location:admin.php?page=myplug/muyplg.php&info=upd");
		//exit;
	}*/

	$act=$_REQUEST["act"];
	if($act=="upd")
	{
		$recid=$_REQUEST["id"];
		echo $sSQL="SELECT  parishioner.*, parish_ward.ward_name,
					family.family_no,family.ward_no
					FROM  parishioner 
					INNER JOIN family ON  parishioner.family_id = family.id
					INNER JOIN parish_ward ON  family.ward_no = parish_ward.ward_no	
					WHERE parishioner.id=$recid";
		
		$result = mysql_query($sSQL) or die ('Error, query failed');
		if (mysql_num_rows($result) > 0 )
		{
			if($row = mysql_fetch_assoc($result))
			{
				$id        					= $row['id'];
				$name        		        = $row['name'];
				$baptismal_name        		= $row['baptismal_name'];
				$date_of_birth        		= $row['date_of_birth'];
				$baptism_date        		= $row['baptism_date'];
				$name_father        		= $row['name_father'];
				$name_mother        		= $row['name_mother'];
				$phone_no        			= $row['phone_no'];
				$mob_no        				= $row['mob_no'];
				$e_mail        				= $row['e_mail'];
				$blood_group        		= $row['blood_group'];
				$present_address			= $row['family_id'];
				
				$family_id					= $row['family_id'];
				$family_no					= $row['family_no'];	
				
				$ward_name					= $row['ward_name'];
				$ward_no					= $row['ward_no'];	
			
				$save_button = "save_customer_info";
				$hidval	   = 2;
			}
		}
	}
	else
	{
		$btn	     ="Add New Member";
		$save_button = "create_new_user";
		$id        = "";
		$name  	   = "";
		$email     = "";
		$contact   = "";
		$add       = "";
		$profile_page = "";
		$sort_order = "";
		$hidval	   = 1;
	}
?>

<style type="text/css">
#wpbody-content #dashboard-widgets #postbox-container-2, #wpbody-content #dashboard-widgets #postbox-container-3, #wpbody-content #dashboard-widgets #postbox-container-4{
float:left!important;width:50%!important;
}
</style>
<div xmlns="http://www.w3.org/1999/xhtml" class="wrap nosubsub">

<div xmlns="http://www.w3.org/1999/xhtml" class="wrap nosubsub">
  <div class="icon32" id="icon-edit"><br/>
  </div>
  <div class="wrap">
    <h2>User Informations</h2>
  	<form class="validate" action="admin.php?page=member_add&type=<?php echo $type;?>&act=<?php echo $act;?>" enctype="multipart/form-data" method="post" id="addtag">
      <div id="dashboard-widgets" class="metabox-holder">

        <div id="postbox-container-3" class="postbox-container">
          <div id="normal-sortables" class="meta-box-sortables ui-sortable">
            <div id="dashboard_activity" class="postbox" style="display: block;">
              <h3 class="wp-menu-image dashicons-before dashicons-admin-users" style="border-bottom: 1px solid #eee;"><span> BASIC  INFORMATION</span></h3>
        
              <div class="inside">
                <div id="activity-widget">
                  <div id="published-posts" class="activity-block">
                    <table width="100%">
                      <tr>
                        <td width="38%" align="left"><h4>Name:</h4></td>
                        <td width="2%" align="center" valign="top"> </td>
                        <td width="60%" align="left"><h4>
                           <input type="text" size="40" id="name" name="name" value="<?php echo $name; ?>"/>
                          </h4></td>
                      </tr>
                      <tr>
                        <td align="left"><h4>Baptismal Name:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           <input type="text" size="40" id="baptismal_name" name="baptismal_name" value="<?php echo $baptismal_name; ?>"/>
                          </h4></td>
                      </tr>
					  
					  <tr>
                        <td align="left"><h4>Father Name:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <input type="text" size="40" id="name_father" name="name_father" value="<?php echo $name_father; ?>"/>
                          </h4></td>
                      </tr>
					  
					  <tr>
                        <td align="left"><h4>Mother Name:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           <input type="text" size="40" id="name_mother" name="name_mother" value="<?php echo $name_mother; ?>"/> 
                          </h4></td>
                      </tr>
                      <tr>
                        <td align="left"><h4>Family Position	:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                          <input type="text" size="40" id="family_position" name="family_position" value="<?php echo $family_position; ?>"/>  
                          </h4></td>
                      </tr>
                      <tr>
                        <td align="left"><h4>Date of birth:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                 
							   <input type="text" size="40" id="date_of_birth" name="date_of_birth" value="<?php echo $date_of_birth; ?>"/>  
                          </h4></td>
                      </tr>
                      <tr>
                        <td align="left"><h4>Confirmation Date:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           	   <input type="text" size="40" id="confirmation_date" name="confirmation_date" value="<?php echo $confirmation_date; ?>"/> 
                          </h4></td>
                      </tr>
					  <tr>
                        <td align="left"><h4>Phone no:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           	   <input type="text" size="40" id="phone_no" name="phone_no" value="<?php echo $phone_no; ?>"/> 
                          </h4></td>
                      </tr>
					  
					  <tr>
                        <td align="left"><h4>Mob no:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           	   <input type="text" size="40" id="mob_no" name="mob_no" value="<?php echo $mob_no; ?>"/> 
                          </h4></td>
                      </tr>
					   <tr>
                        <td align="left"><h4>Email:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           	   <input type="text" size="40" id="e_mail" name="e_mail" value="<?php echo $e_mail; ?>"/> 
                          </h4></td>
                      </tr>
					  
					   <tr>
                        <td align="left"><h4>Blood group:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           	   <input type="text" size="40" id="blood_group" name="blood_group" value="<?php echo $blood_group; ?>"/> 
                          </h4></td>
                      </tr>
					  
					   <tr>
                        <td align="left"><h4>Present address:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           	 <textarea name="present_address"><?php echo $present_address; ?></textarea>
                          </h4></td>
                      </tr>
                    
                      
					  
					  
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
     
      
		 <div id="postbox-container-3" class="postbox-container">
          <div id="normal-sortables" class="meta-box-sortables ui-sortable">
            <div id="dashboard_activity" class="postbox" style="display: block;">
              <h3 class="wp-menu-image dashicons-before dashicons-admin-users" style="border-bottom: 1px solid #eee;"><span> OTHER INFORMATION</span></h3>
        
              <div class="inside">
                <div id="activity-widget">
                  <div id="published-posts" class="activity-block">
                    <table width="100%">
                    
                      <tr>
                        <td align="left"><h4>Family No</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                           <input type="text" name="family_no" value="<?php  echo $family_no;?>">
                          </h4></td>
                      </tr>
                      <tr>
                        <td align="left"><h4>Ward:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
							<select name="ward_number">
                            <?php   echo $objWard ->getWardSelect($ward_no);?> 
							</select>
                          </h4></td>
                      </tr>
					  
					   <tr>
                        <td align="left"><h4>Occupation:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <?php  echo $occupation;?>
                          </h4></td>
                      </tr>
                      <tr>
                        <td align="left"><h4>Religious Study	:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <?php  echo $religious_study	;?>
                          </h4></td>
                      </tr>
					  
					  
					  <tr>
                        <td align="left"><h4>Phone :</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <?php  echo $phone_no;?>
                          </h4></td>
                      </tr>
					  <tr>
                        <td align="left"><h4>MObile No	:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <?php  echo $mob_no;?>
                          </h4></td>
                      </tr>
					  <tr>
                        <td align="left"><h4>Email	:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <?php  echo $e_mail;?>
                          </h4></td>
                      </tr>
					  <tr>
                        <td align="left"><h4>Blood Group	:</h4></td>
                        <td align="center" valign="top"> </td>
                        <td align="left"><h4>
                            <?php  echo $blood_group;?>
                          </h4></td>
                      </tr>
					  
					  
					  
					  
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
  
  <p class="submit">
						<input type="submit" value="Save Info" class="button" id="submit" name="<?php echo $save_button; ?>"/>
						<input type="hidden" name="addme" value=<?php echo $hidval;?> >
						<input type="hidden" name="user_id" value=<?php echo $id;?> >
					</p>
	</div>
	
	
	
	
	
	
	
	</form
	
    <!-- dashboard-widgets-wrap --> 
    
  </div>
 
</div>

<?php if($type=='customer'){ ?>
<script>
jQuery("#toplevel_page_admin-page-memberlist-type-customer" ).removeClass();
jQuery("#toplevel_page_admin-page-memberlist-type-customer" ).addClass( "wp-has-submenu wp-has-current-submenu wp-menu-open menu-top menu-icon-generic toplevel_page_myplug/muyplg" );
jQuery("#toplevel_page_admin-page-memberlist-type-customer").css("background-color", "#0074a2");
</script>
<?php } else {?>

<script>
jQuery("#toplevel_page_admin-page-memberlist-type-photographers" ).removeClass();
jQuery("#toplevel_page_admin-page-memberlist-type-photographers" ).addClass( "wp-has-submenu wp-has-current-submenu wp-menu-open menu-top menu-icon-generic toplevel_page_myplug/muyplg" );
jQuery("#toplevel_page_admin-page-memberlist-type-photographers").css("background-color", "#0074a2");
</script>
<?php } ?>